//
//  LoginViewController.m
//  登陆注册
//
//  Created by 吴俊杰 on 2016/11/14.
//  Copyright © 2016年 吴俊杰. All rights reserved.
//

#import "JPLoginViewController.h"
#import <SVProgressHUD.h>
#import "InterfaceTool.h"

@interface JPLoginViewController ()

// 电话号码输入框
@property (weak,nonatomic)UITextField *phoneTextField;
// 密码输入框
@property (weak,nonatomic)UITextField *secretTextField;

@end

@implementation JPLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupSubviews];
    // 记住账号
    self.phoneTextField.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"phone"];
}

/**
 初始化子视图
 */
- (void)setupSubviews {
    // 手机号码TextField
    UITextField *phoneTextField = [[UITextField alloc] init];
    [self.view addSubview:phoneTextField];
    self.phoneTextField = phoneTextField;
    
    // 设置属性
    phoneTextField.placeholder = @"请输入您的手机号码";
    phoneTextField.borderStyle = UITextBorderStyleRoundedRect;
    phoneTextField.font = [UIFont systemFontOfSize:14.0];
    
    // 自动布局
    CGFloat textFieldLR = 40;
    CGFloat textFieldTop = 200;
    CGFloat textFieldSpace = 20;
    [phoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view).offset(textFieldLR);
        make.right.equalTo(self.view).offset(-textFieldLR);
        make.top.equalTo(self.view).offset(textFieldTop);
    }];
    
    //密码TextField
    UITextField *secretTextField = [[UITextField alloc] init];
    [self.view addSubview:secretTextField];
    self.secretTextField = secretTextField;
    
    // 设置属性
    secretTextField.placeholder = @"请输入密码";
    secretTextField.borderStyle = UITextBorderStyleRoundedRect;
    secretTextField.font = [UIFont systemFontOfSize:14.0];
    // 隐藏密码
    secretTextField.secureTextEntry = YES;
    // 自动布局
    [secretTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view).offset(textFieldLR);
        make.right.equalTo(self.view).offset(-textFieldLR);
        make.top.equalTo(phoneTextField.mas_bottom).offset(textFieldSpace);
    }];

    // 切换登陆方式按钮
    UIButton *changeLoginModeBtn = [[UIButton alloc] init];
    [self.view addSubview: changeLoginModeBtn];
    // 设置按钮属性
    [changeLoginModeBtn setTitle:@"通过短信验证码登陆" forState:UIControlStateNormal];
    [changeLoginModeBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    changeLoginModeBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
    // 自动布局
    [changeLoginModeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.view);
        make.top.equalTo(secretTextField.mas_bottom).offset(40);
    }];
    
    // 登陆按钮
    UIButton *loginBtn = [[UIButton alloc] init];
    [self.view addSubview:loginBtn];
    // 设置按钮属性
    [loginBtn setBackgroundImage:[UIImage imageNamed:@"buttonBackground"] forState:UIControlStateNormal];
    [loginBtn setTitle:@"登陆" forState:UIControlStateNormal];
    // 自动布局
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(changeLoginModeBtn.mas_bottom).offset(40);
        make.left.right.equalTo(secretTextField);
    }];
    //添加登陆的点击事件
    [loginBtn addTarget:self action:@selector(loginBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    //分割线
    UIView *lineView = [[UIView alloc] init];
    [self.view addSubview:lineView];
    lineView.backgroundColor = [UIColor grayColor];
    // 自动布局
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(loginBtn.mas_bottom).offset(25);
        make.width.mas_equalTo(1);
        make.height.mas_equalTo(30);
        make.centerX.equalTo(self.view);
    }];
    
    // 注册按钮
    UIButton *registBtn = [[UIButton alloc] init];
    [self.view addSubview:registBtn];
    // 设置按钮属性
    [registBtn setTitle:@"注册账号" forState:UIControlStateNormal];
    [registBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [registBtn.titleLabel setFont:[UIFont systemFontOfSize:14.0]];
    // 自动布局
    [registBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(lineView.mas_left).offset(-30);
        make.centerY.equalTo(lineView);
    }];
    
    // 找回密码按钮
    UIButton *findSecretBtn = [[UIButton alloc] init];
    [self.view addSubview:findSecretBtn];
    // 设置按钮属性
    [findSecretBtn setTitle:@"找回密码" forState:UIControlStateNormal];
    [findSecretBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [findSecretBtn.titleLabel setFont:[UIFont systemFontOfSize:14.0]];
    // 自动布局
    [findSecretBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(lineView.mas_right).offset(30);
        make.centerY.equalTo(lineView);
    }];
    
    // 微信账号登陆按钮
    UIButton *WeChatLoginBtn = [[UIButton alloc] init];
    [self.view addSubview:WeChatLoginBtn];
    [WeChatLoginBtn setImage:[UIImage imageNamed:@"UMS_wechat_icon"] forState:UIControlStateNormal];
    // 自动布局
    [WeChatLoginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(lineView.mas_bottom).offset(40);
        make.centerX.equalTo(self.view);
        make.width.height.mas_equalTo(30);
    }];
    
    //QQ账号登陆按钮
    UIButton *QQLoginBtn = [[UIButton alloc] init];
    [self.view addSubview:QQLoginBtn];
    [QQLoginBtn setImage:[UIImage imageNamed:@"UMS_qq_icon"] forState:UIControlStateNormal];
    // 自动布局
    [QQLoginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(WeChatLoginBtn.mas_left).offset(-30);
        make.centerY.equalTo(WeChatLoginBtn);
        make.width.height.mas_equalTo(30);
    }];
    
    //微博账号登陆按钮
    UIButton *WeiboLoginBtn = [[UIButton alloc] init];
    [self.view addSubview:WeiboLoginBtn];
    [WeiboLoginBtn setImage:[UIImage imageNamed:@"UMS_sina_icon"] forState:UIControlStateNormal];
    // 自动布局
    [WeiboLoginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(WeChatLoginBtn.mas_right).offset(30);
        make.centerY.equalTo(WeChatLoginBtn);
        make.width.height.mas_equalTo(30);
    }];
}

/**
 登陆按钮点击事件
 */
- (void)loginBtnAction:(UIButton *)sender {
    // 设置弹窗显示时间
    [SVProgressHUD setMinimumDismissTimeInterval:1.0];
    //获取密码
    NSString *secret = [SSKeychain passwordForService:[NSBundle mainBundle].bundleIdentifier account:self.phoneTextField.text];
    if ([self.secretTextField.text isEqualToString:secret]) {
        
        // 提示正在登录中
        [SVProgressHUD showWithStatus:@"正在登录中···"];
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];

        //密码正确, 发送登录请求
        [[InterfaceTool sharedInterfaceTool] getLoginSuccess:^(id response) {
            if (response != 0) {
                NSDictionary *responseDict = response;
                if ([responseDict[@"msg"] isEqualToString: @"OK"]) {
                    
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"loginSuccess" object:nil];
                }else {
                    [SVProgressHUD showErrorWithStatus:@"登录失败!"];
                }
            }else {
                [SVProgressHUD showErrorWithStatus:@"登录失败!"];
            }
        } failure:^(NSError *error) {
            [SVProgressHUD showErrorWithStatus:@"登录超时!"];
        }];
        
    }else {
        [SVProgressHUD showErrorWithStatus:@"账号或密码错误!"];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
